/**
 * 将总秒数转换为天、时、分、秒的格式
 * @param totalSeconds 总秒数
 * @returns 格式化的时间字符串，如 "2天 3小时 45分钟 30秒"
 */
export const formatSeconds = (totalSeconds: number | string): string => {
  let seconds: number;

  if (typeof totalSeconds === 'string') {
    seconds = Number(totalSeconds);
    if (isNaN(seconds)) {
      console.warn('formatSeconds: 传入的参数不是有效的数字');
      return "0秒";
    }
  } else {
    seconds = totalSeconds;
  }

  if (seconds < 0) {
    console.warn('formatSeconds: 秒数不能为负数');
    return "0秒";
  }

  seconds = Math.floor(seconds);

  if (seconds === 0) return "0秒";

  const secondsInMinute = 60;
  const secondsInHour = 60 * secondsInMinute;
  const secondsInDay = 24 * secondsInHour;

  const days = Math.floor(seconds / secondsInDay);
  const remainingAfterDays = seconds % secondsInDay;

  const hours = Math.floor(remainingAfterDays / secondsInHour);
  const remainingAfterHours = remainingAfterDays % secondsInHour;

  const minutes = Math.floor(remainingAfterHours / secondsInMinute);
  const secs = remainingAfterHours % secondsInMinute;

  const parts: string[] = [];

  if (days > 0) {
    parts.push(`${days}天`);
  }

  if (hours > 0) {
    parts.push(`${hours}小时`);
  }

  if (minutes > 0) {
    parts.push(`${minutes}分钟`);
  }

  if (secs > 0 || parts.length === 0) {
    parts.push(`${secs}秒`);
  }

  return parts.join(" ");
}
